namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Web;
    using System.Web.Mvc;
    using SimpleWebServices.Globalization;

    public abstract class ResourceController : Controller, IControlResource
    {
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "ASP.NET MVC requires the method to be an instance member.")]
        public ActionResult LanguageNegotiation()
        {
            string language = "en"; /// TODO: Implement Accept-Language negotiation

            new CultureInfo(language).SetCurrentCulture();

            HttpRequestBase request = HttpContextFactory.Current.Request;

            string location = string.Concat(request.Path, ".", language, request.RawQueryString());

            return new SeeOtherResult(location);
        }

        public ActionResult ContentNegotiation(CultureInfo language)
        {
            if (null == language)
            {
                throw new ArgumentNullException("language");
            }
            else if (CultureInfo.InvariantCulture == language)
            {
                throw new ArgumentOutOfRangeException("language");
            }

            language.SetCurrentCulture();

            HttpRequestBase request = HttpContextFactory.Current.Request;
            Accept accept = request.Headers["Accept"];

            return accept.Negotiate(request, this.GetType());
        }

        [SuppressMessage("Microsoft.Design", "CA1061:DoNotHideBaseClassMethods", Justification = "The base method is not marked virtual.")]
        public ViewResult View(CultureInfo language, object model)
        {
            language.SetCurrentCulture();

            return this.View(model);
        }
    }
}